package cn.yunhe.dao.impl;

import cn.yunhe.dao.HouseDao;
import cn.yunhe.model.*;
import cn.yunhe.util.Pagination;
import org.hibernate.*;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/*spring注解*/
@Repository(value = "houseDao")
public class HouseDaoImpl extends HibernateDaoSupport implements HouseDao {

    /*@Resource
    public void setSesssionFactory(SessionFactory sesssionFactory){
        super.setSessionFactory(sesssionFactory);
    }*/
    /**/
    @Autowired
    @Qualifier("sessionFactory")
    public void setSessionFactory111(SessionFactory sessionFactory){
        super.setSessionFactory(sessionFactory);
    }

    @Override
    public boolean insertHouse(House house) {
        getHibernateTemplate().save(house);
        return false;
    }

    @Override
    public boolean updateHouse(House house) {
        getHibernateTemplate().update(house);
        return false;
    }

    @Override
    public boolean deleteHouse(Integer hid) {
        getHibernateTemplate().delete(this.getById(hid));
        return false;
    }

    @Override
    public House getById(Integer hid) {
        House house=getHibernateTemplate().get(House.class,hid);
        return house;
    }

    @Override
    public List<House> searchHouse(String hql) {
        List<House>houseList=getHibernateTemplate().find(hql);
        return houseList;
    }

    @Transactional
    @Override
    public List<House>searchHouse(House house, Pagination<House> pagination){
        Map<String,Object>queryProperties=new HashMap<>();
        StringBuilder hql=new StringBuilder();

        //hql.append("from House1 where 1=1 ");
        //hql.append(" from House1 h inner join h.member inner join h.type inner join h.street inner join h.street.district where 1=1 ");
        hql.append(" from House1 h   ");
        hql.append(" inner join h.member ");
        hql.append(" inner join h.type ");
        hql.append(" inner join h.street ");
        hql.append(" inner join h.street.district ");
        hql.append(" where 1=1 ");
        if (house!=null){
            if (house.getHid()!=null&&house.getHid()>0){
                hql.append("and hid=:hid ");
                queryProperties.put("hid",house.getHid());
            }
            if (house.getHtitle()!=null&&house.getHtitle().trim().length()>0){
                hql.append("and htitle like :htitle ");
                queryProperties.put("htitle","%"+house.getHtitle()+"%");
            }
            if (house.getHprice()!=null&&house.getHprice()>0){
                hql.append("and hprice>=:hprice ");
                queryProperties.put("hprice",house.getHprice());
            }
            if (house.getHfloorage()!=null&&house.getHfloorage()>0){
                hql.append("and hfloorage>=:hfloorage ");
                queryProperties.put("hfloorage",house.getHfloorage());
            }
            if (house.getHpubdate()!=null&&house.getHpubdate().trim().length()>0){
                hql.append("and hpubdate like :hpubdate ");
                queryProperties.put("hpubdate","%"+house.getHpubdate()+"%");
            }
            if (house.getHflag()!=null&&house.getHflag()>=0){
                hql.append("and hflag=:hflag ");
                queryProperties.put("hflag",house.getHflag());
            }
            if (house.getType()!=null){
                //if (house.getType().getTname()!=null&&house.getType().getTname().trim().length()>0){
                if (house.getType().getTid()!=null&&house.getType().getTid()>0){
                    hql.append("and h.type.tid=:tid  ");
                    queryProperties.put("tid",house.getType().getTid());
                }
            }
            if (house.getStreet()!=null){
                //if (house.getStreet().getSname()!=null&&house.getStreet().getSname().trim().length()>0){
                if (house.getStreet().getSid()!=null&&house.getStreet().getSid()>0){
                    hql.append("and h.street.sid=:sid  ");
                    queryProperties.put("sid",house.getStreet().getSid());
                }
            }
            if (house.getStreet()!=null&&house.getStreet().getDistrict()!=null){
                //if (house.getStreet().getDistrict().getDname()!=null&&house.getStreet().getDistrict().getDname().trim().length()>0){
                if (house.getStreet().getDistrict().getDid()!=null&&house.getStreet().getDistrict().getDid()>0){
                    hql.append("and h.street.district.did=:did  ");
                    queryProperties.put("did",house.getStreet().getDistrict().getDid());
                }
            }
        }
        hql.append("order by hid ");
        System.out.println("==========="+hql);
        Query query =getHibernateTemplate().
                getSessionFactory().getCurrentSession()
                .createQuery(hql.toString());

        query.setProperties(queryProperties);
        if (pagination!=null&&pagination.getStartIndex()>=0&&pagination.getMaxRows()>0){
            query.setFirstResult(pagination.getStartIndex());
            query.setMaxResults(pagination.getMaxRows());
        }

        List<House>houseList=new ArrayList<>();
        List<Object[]>objects=query.list();
        System.out.println(objects.size());
        for (Object[] objects1:objects){
            System.out.print(objects1[0]+"\t");
            System.out.print(objects1[1]+"\t");
            System.out.print(objects1[2]+"\t");
            System.out.print(objects1[3]+"\t");
            System.out.print(objects1[4]+"\t");
            System.out.println("=========");

            House house1= (House) objects1[0];
            Member member= (Member) objects1[1];
            Type type= (Type) objects1[2];
            Street street= (Street) objects1[3];
            District district= (District) objects1[4];
            street.setDistrict(district);
            house1.setMember(member);
            house1.setType(type);
            house1.setStreet(street);
            houseList.add(house1);
        }

        return houseList;
    };

    @Override
    public List<House> searchHouse(final House house) {
        List<House>houseList=getHibernateTemplate().executeFind(new HibernateCallback<List<House>>() {
            @Override
            public List<House> doInHibernate(Session session) throws HibernateException, SQLException {
                Criteria criteria=session.createCriteria(House.class);
                if (house!=null){
                    if (house.getHid()>0){
                        criteria.add(Restrictions.eq("hid",house.getHid()));
                    }
                    if (house.getHtitle()!=null&&house.getHtitle().trim().length()>0){
                        criteria.add(Restrictions.like("htitle",house.getHtitle(),MatchMode.ANYWHERE));
                    }
                    if (house.getHprice()>0){
                        criteria.add(Restrictions.ge("hprice",house.getHprice()));
                    }
                    if (house.getHfloorage()>0){
                        criteria.add(Restrictions.ge("hfloorage",house.getHfloorage()));
                    }
                    if (house.getHpubdate()!=null&&house.getHpubdate().trim().length()>0){
                        criteria.add(Restrictions.like("hpubdate",house.getHpubdate(),MatchMode.ANYWHERE));
                    }
                }
                List<House>houseList111=criteria.list();
                return houseList111;
            }
        });
        return houseList;
    }
}


  /*  private int hid;
    private String htitle;//标题
    private double hprice;//价格
    private double hfloorage;//房屋面积
    private String hpubdate;//房产证日期
    private String hcontact;//联系方式
    private String hdesc;//具体描述
    private int hflag;//信息当前是否有效

    private Member member=new Member();
    private Type type=new Type();
    private Street street=new Street();*/